package com.wang;


import org.apache.dubbo.common.URL;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.utils.ReferenceConfigCache;
import org.apache.dubbo.rpc.RpcContext;
import org.apache.dubbo.rpc.service.GenericService;

import java.util.HashMap;
import java.util.Map;

/**
 * Unit test for simple App.
 */
public class App {
    public static void main(String[] args) {
        ApplicationConfig applicationConfig = new ApplicationConfig();
        applicationConfig.setName("dubbo-consumer");
        RegistryConfig registryConfig = new RegistryConfig();
        //registryConfig.setAddress("nacos://127.0.0.1:8848");
        registryConfig.setAddress("N/A");
        ReferenceConfig<GenericService> reference = new ReferenceConfig<>();
        reference.setApplication(applicationConfig);
        reference.setRegistry(registryConfig);
        reference.setInterface("com.wang.service.IDemoService");
        //设置为泛型调用，类型为true
        reference.setGeneric("true");
        reference.setCheck(false);
        reference.setGroup("dev2");
        reference.setVersion("1.0.0");
        reference.setTimeout(3000);
        reference.toUrls().add(new URL("dubbo","127.0.0.1",20081));
        RpcContext.getContext().setAttachment("timeout", "3000");
        ReferenceConfigCache cache = ReferenceConfigCache.getCache();
        GenericService genericService = cache.get(reference);

        Object result = genericService.$invoke("getMsg", new String[]{"java.lang.String"}, new String[]{"dev"});
        System.out.println(result);
        //POJO参数转换成Map
        Map<String,Object> map = new HashMap<>();
        map.put("class","com.wang.dto.Pojo");
        map.put("id","1989");
        map.put("name","wang");
        //发起泛型调用
        result = genericService.$invoke("testPojo", new String[]{"com.wang.dto.Pojo"}, new Object[]{map});
        System.out.println(result);
        RpcContext.getContext().clearAttachments();
        RpcContext.getContext().remove("_trace_id_");
    }

}
